package edu.pitt.dbmi.odie.gapp.gwt.client.analysis.creation;

import com.smartgwt.client.data.DataSource;
import com.smartgwt.client.data.Record;
import com.smartgwt.client.data.fields.DataSourceTextField;
import com.smartgwt.client.types.DSDataFormat;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.grid.ListGrid;
import com.smartgwt.client.widgets.grid.ListGridRecord;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickEvent;
import com.smartgwt.client.widgets.grid.events.RecordDoubleClickHandler;
import com.smartgwt.client.widgets.layout.VLayout;

import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.ODIE_AnalysisPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.util.ODIE_ClientUtils;

public class ODIE_AnalysisCreationPanel extends VLayout implements ClickHandler {

	private ListGrid directoryListGrid = null ;
	
	private ButtonItem[] buttonItems = new ButtonItem[1];

	private ODIE_AnalysisPanel analysisPanel;

	public ODIE_AnalysisCreationPanel() {
		setLayoutMargin(5);
		setMembersMargin(10);
		buildNewAnalysisGroup(this);
	}

	private void buildNewAnalysisGroup(VLayout main) {
		DataSource directoriesDS = buildDirectoryDataSource();
		this.directoryListGrid = ODIE_ClientUtils.buildListGrid();
		this.directoryListGrid.setIsGroup(true) ;
		this.directoryListGrid.setGroupTitle("Directories");
		this.directoryListGrid.setDataSource(directoriesDS);
		this.directoryListGrid.addRecordDoubleClickHandler(buildDirectoryNameRecordDoubleClickListener()) ;
		this.directoryListGrid.setAutoFetchData(true) ;
		this.directoryListGrid.setCanEdit(false);
		DynamicForm actionForm = buildActionsPanel() ;
		main.addMember(this.directoryListGrid) ;
		main.addMember(actionForm) ;
	}

	public DynamicForm buildActionsPanel() {
		buttonItems[0] = ODIE_ClientUtils.buildButtonItem("Annotate", this);
		return ODIE_ClientUtils.buildButtonGroup("Actions", buttonItems);
	}

	private DataSource buildDirectoryDataSource() {
		DataSource dataSource = new DataSource();
		dataSource.setDataFormat(DSDataFormat.XML);
		dataSource.setRecordXPath("//atom:entry");
		dataSource.setDataURL("odie_client/document");
		DataSourceTextField idField = new DataSourceTextField("id", "id");
		idField.setHidden(true);
		DataSourceTextField displayLabelField = new DataSourceTextField(
				"title", "Title");
		displayLabelField.setTitle(null) ;
		dataSource.setFields(idField, displayLabelField);
		return dataSource;
	}

	private RecordDoubleClickHandler buildDirectoryNameRecordDoubleClickListener() {
		return new RecordDoubleClickHandler() {
			public void onRecordDoubleClick(RecordDoubleClickEvent event) {
				Record record = event.getRecord();
				String directoryHandle = record.getAttribute("id");
				String title = record.getAttribute("title");
				ODIE_AnalysisCreationPanel.this.analysisPanel.createAnalysis(directoryHandle, title);
			}
		};
	}

	public void onClick(ClickEvent event) {
		FormItem formItem = event.getItem();
		if (formItem == buttonItems[0]) {
			ListGridRecord record = this.directoryListGrid.getSelectedRecord() ;
			String directoryHandle = record.getAttribute("id");
			String title = record.getAttribute("title");
			this.analysisPanel.createAnalysis(directoryHandle, title);
		}
	}

	public void setAnalysisPanel(ODIE_AnalysisPanel analysisPanel) {
		this.analysisPanel = analysisPanel ;
	}

	public void processLogin() {
		this.directoryListGrid.fetchData() ;
		redraw() ;
	}



	
}
